package fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;

import com.example.administrator.customview.R;

import java.util.List;

import adapter.FileManagerFileListAdapter;
import moudle.FileManagerMoudle;
import view.TranslateView;

/**
 * Created by eyesee on 2017/4/24.
 */

public class FilemanagerFileList extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {

    private View mView;
    private GridView mGridView;
    private ImageButton mButton;
    private MenuFragment mMenuFragment;
    private FileManagerVideoList mVideolist;
    private SortListener mSortListener;
    private FileManagerMoudle mMoudle;
    private TranslateView mBord;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.filemanager_filelist, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        mGridView = (GridView) view.findViewById(R.id.filelist_gv);
        mButton = (ImageButton) view.findViewById(R.id.filelist_bt);
        mBord = (TranslateView) view.findViewById(R.id.bord);

        mBord.setTvScreen(true);

        mMenuFragment = new MenuFragment();
        mMoudle = new FileManagerMoudle();
        List<String> data = mMoudle.getData();
        FileManagerFileListAdapter adapter = new FileManagerFileListAdapter(getActivity(),data);
        mGridView.setAdapter(adapter);

        mButton.setOnClickListener(this);
        mGridView.setOnItemSelectedListener(this);
        mGridView.setOnItemClickListener(this);
    }
    @Override
    public void onClick(View v) {
        //按菜单键时弹出菜单
        mMenuFragment.show(getFragmentManager(),"");
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //选择item时的平移动画
        mBord.setVisibility(View.VISIBLE);
        mBord.setSelectView(view);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击文件夹时,获取文件夹的path并执行扫描,获取文件夹内文件,重新setAdapter并刷新adapter
        //如果点击的是video,携带video路径跳转到播放器页面
        //从存储file信息的集合中按positon取得对应的file,获取path,并可以判断file是文件夹还是还是视频
    }



    /**时间排序,使用接口对象调用接口中方法*/
    public void TimeSortListener(){
        mSortListener.TimeSort(mMoudle);
    }

    /**名称排序,使用接口对象调用接口中方法*/
    public void NameSortListener(){
        mSortListener.NameSort(mMoudle);
    }

    //声明方法,传入接口对象
    public void SetSortListener(SortListener sortListener){
        mSortListener = sortListener;
    }

    //声明接口
    public interface SortListener{
        void TimeSort(FileManagerMoudle fileManagerMoudle);
        void NameSort(FileManagerMoudle fileManagerMoudle);
    }
}
